﻿using CommunicationProtocol.dto.command.client;
using CommunicationProtocol.dto.command.server;
using server.filters;
using server.threadContext;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;

namespace server.filter
{
    internal class LoginFilter : NetFilter
    {

        private ConditionalWeakTable<ClientMetaData, ConcurrentStack<Object>> notLoginedRequestCache = new ConditionalWeakTable<ClientMetaData, ConcurrentStack<Object>>();

        public void invoke(object buffer, InvocationChain invocationChain)
        {
            

            ClientMetaData clientMetaData = ClientMetaDataContext.get();


            if(clientMetaData is null ){
                Console.WriteLine("没有用户的元数据，禁止房问");
                return;
            }

            ClientMetaData clientMeta = ClientMetaDataContext.get();

            ConcurrentStack<object> bufferList = notLoginedRequestCache.GetValue(clientMeta, meta => {

                return new ConcurrentStack<object>();
            });
            
            byte[] bytes = (byte[])buffer;
            
            if (  bytes[0] == 4) {


                lock (clientMeta)
                {
                    if (clientMetaData.userId is null)
                    {
                        Console.WriteLine("没有用户的id信息，用户尚未登录");
                        
                        LoginRequest loginRequest = new LoginRequest(new ArraySegment<byte>(bytes, 1, bytes.Length - 1).ToArray());


                        if (string.IsNullOrEmpty(loginRequest.V))
                        {

                            Console.WriteLine("登录失败");
                            return;
                        }
                        Console.WriteLine(loginRequest.V + "登录成功");

                        clientMetaData.userId = loginRequest.V;

                 

                    }
                
                }
                
                while (!bufferList.IsEmpty) {
                    if (bufferList.TryPop(out buffer))
                    {
                        new InvocationChain(invocationChain).invoke(buffer);
                    }

                }
            }
            else
            {
                
                if(clientMetaData.userId is null) {
                    
                    Console.WriteLine("要求用户登录");

                    bufferList.Push(buffer);

                    new InvocationChain(clientMetaData.outband).invoke(
                        new RequiredToLoginCommand()
                    );
                }
                else
                {
                    invocationChain.invoke(buffer);
                }

                /*if (bytes[0] == 4)
                {
                    Console.WriteLine($"{clientMetaData.userId} 已经登录，但还想重复登录，拒绝访问");
                    return;
                }*/

               
            }
        }
    }
}
